package com.situ.company.controller;

import com.situ.company.entity.DeptCount;
import com.situ.company.entity.User;
import com.situ.company.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
@RequestMapping("/user")
public class UserController {
    // 注入过来使用
    @Autowired
    UserMapper userMapper;

    // localhost:8080/user/selectAll
    @RequestMapping("/selectALL")
    @ResponseBody // 返回json格式的数据
    public ArrayList<User> selectALL() {
        System.out.println("UserController.selectALL");
        ArrayList<User> list = userMapper.selectAll();
        return list;
    }

    // localhost:8080/user/selectAll
    @RequestMapping("/selectAll")
    public String selectAll(Model model) {
        System.out.println("UserController.selectAll");
        ArrayList<User> list = userMapper.selectAll();
        // 把查找出来的数据放到内存里面
        model.addAttribute("list", list);
        // 转发到界面展示   "/"代表的就是webapp这个目录
        return "/user_list.jsp";
    }

    // /user/deleteById?id=12
    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        System.out.println("UserController.deleteById id: " + id);
        userMapper.deleteById(id);

        // 删除完了之后，重定向到: /user/selectAll
        return "redirect:/user/selectAll";
    }

    @RequestMapping("/add")
    public String add(User user) {
        System.out.println(user);
        userMapper.add(user);

        // 删除完了之后，重定向到: /user/selectAll
        return "redirect:/user/selectAll";
    }

    @RequestMapping("/login")
    public String login(String name, String password, HttpSession session) {
        System.out.println("UserController.login");
        User user = userMapper.login(name, password);
        if (user != null) {// 代表有这个用户
            // 将用户的登录信息放到服务器的session里面,作为登录的凭证
            session.setAttribute("user", user);
            return "redirect:/"; // 首页
        } else { // 没有这样的用户
            return "redirect:/login.jsp"; // 登录界面
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        // 从session里面把登录的凭证移除掉
        session.removeAttribute("user");
        // 注销之后跳转到登录界面
        return "redirect:/login.jsp";
    }

    @RequestMapping("/selectById")
    public String selectById(Integer id, Model model) {
        System.out.println("UserController.selectById");
        User user = userMapper.selectById(id);
        model.addAttribute("user", user);
        return "/user_update.jsp";
    }

    @RequestMapping("/update")
    public String update(User user) {
        System.out.println(user);
        userMapper.update(user);

        // 删除完了之后，重定向到: /user/selectAll
        return "redirect:/user/selectAll";
    }

    @RequestMapping("/selectDeptCount")
    @ResponseBody
    public ArrayList<DeptCount> selectDeptCount() {
        System.out.println("UserController.selectDeptCount");
        ArrayList<DeptCount> list = userMapper.selectDeptCount();
        return list;
    }
}
